﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.FILAPI;
using System.Xml;
using System.Text;

namespace FilStartUps
{
    public partial class rss : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Convert.ToInt32(Request.QueryString["ID"].Trim()))
            {
                    //Latest Articles
                case 1: GetLatestArticleRSS();
                    break;
                    //Latest News
                case 2: GetLatestNewsRSS();
                    break;
                    //Latest Events
                case 3: GetLastestEventsRSS();
                    break;
                    //Featured Articles
                case 4: GetFeaturedArticlesRSS();
                    break;

            }
        }

        private void GetLastestEventsRSS()
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            //

            XmlTextWriter writer = new XmlTextWriter(Response.OutputStream,
              Encoding.UTF8);
            writer.WriteStartDocument();

            // The mandatory rss tag
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            // The channel tag contains RSS feed details
            writer.WriteStartElement("channel");
            writer.WriteElementString("title", "FilStartUps Newest Submissions");
            writer.WriteElementString("link", "http://www.filstartups.com");
            writer.WriteElementString("description",
              "The latest articles.");
            writer.WriteElementString("copyright",
              String.Format("Copyright {0} FilStartUp. All rights reserved.", DateTime.Today.Year));

            // File Parade image    
            writer.WriteStartElement("image");
            writer.WriteElementString("url",
              "http://www.fileparade.com/Images/logo88x31.png");
            writer.WriteElementString("title",
              "FilStartUps Latest Articles");
            writer.WriteElementString("link", "http://www.filstartups.com");
            writer.WriteEndElement();
            // Objects needed for connecting to the SQL database
            var latest = FilStartUpsAPI.GetLatestEvent();
            foreach (var item in latest)
            {
                writer.WriteStartElement("item");

                writer.WriteElementString("title", item.eventtitle);

                writer.WriteElementString("description", item.eventdescription);

                writer.WriteElementString("link", "hello");

                writer.WriteElementString("pubDate", item.startdate.ToShortDateString());

                writer.WriteEndElement();
            }

            // Close all tags
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
            // Terminate response
            Response.End();

        }

        private void GetFeaturedArticlesRSS()
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            //

            XmlTextWriter writer = new XmlTextWriter(Response.OutputStream,
              Encoding.UTF8);
            writer.WriteStartDocument();

            // The mandatory rss tag
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            // The channel tag contains RSS feed details
            writer.WriteStartElement("channel");
            writer.WriteElementString("title", "FilStartUps Newest Submissions");
            writer.WriteElementString("link", "http://www.filstartups.com");
            writer.WriteElementString("description",
              "The latest articles.");
            writer.WriteElementString("copyright",
              String.Format("Copyright {0} FilStartUp. All rights reserved.", DateTime.Today.Year));

            // File Parade image    
            writer.WriteStartElement("image");
            writer.WriteElementString("url",
              "http://www.fileparade.com/Images/logo88x31.png");
            writer.WriteElementString("title",
              "FilStartUps Latest Articles");
            writer.WriteElementString("link", "http://www.filstartups.com");
            writer.WriteEndElement();
            // Objects needed for connecting to the SQL database
            var latest = FilStartUpsAPI.GetFeaturedPost();
            foreach (var item in latest)
            {
                writer.WriteStartElement("item");

                writer.WriteElementString("title", item.posttitle);

                writer.WriteElementString("description", item.postdescription);

                writer.WriteElementString("link", "hello");

                writer.WriteElementString("pubDate", item.datecreated.ToShortDateString());

                writer.WriteEndElement();
            }

            // Close all tags
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
            // Terminate response
            Response.End();

        }

        private void GetLatestNewsRSS()
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            //

            XmlTextWriter writer = new XmlTextWriter(Response.OutputStream,
              Encoding.UTF8);
            writer.WriteStartDocument();

            // The mandatory rss tag
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            // The channel tag contains RSS feed details
            writer.WriteStartElement("channel");
            writer.WriteElementString("title", "FilStartUps Newest Submissions");
            writer.WriteElementString("link", "http://www.filstartups.com");
            writer.WriteElementString("description",
              "The latest articles.");
            writer.WriteElementString("copyright",
              String.Format("Copyright {0} FilStartUp. All rights reserved.", DateTime.Today.Year));

            // File Parade image    
            writer.WriteStartElement("image");
            writer.WriteElementString("url",
              "http://www.fileparade.com/Images/logo88x31.png");
            writer.WriteElementString("title",
              "FilStartUps Latest Articles");
            writer.WriteElementString("link", "http://www.filstartups.com");
            writer.WriteEndElement();
            // Objects needed for connecting to the SQL database
            var latest = FilStartUpsAPI.GetNewsPost();
            foreach (var item in latest)
            {
                writer.WriteStartElement("item");

                writer.WriteElementString("title", item.posttitle);

                writer.WriteElementString("description", item.postdescription);

                writer.WriteElementString("link", "hello");

                writer.WriteElementString("pubDate", item.datecreated.ToShortDateString());

                writer.WriteEndElement();
            }

            // Close all tags
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
            // Terminate response
            Response.End();

        }

        private void GetLatestArticleRSS()
        {
            //GetLatestArticles();
            // Clear any previous response

            Response.Clear();
            Response.ContentType = "text/xml";
            //

            XmlTextWriter writer = new XmlTextWriter(Response.OutputStream,
              Encoding.UTF8);
            writer.WriteStartDocument();

            // The mandatory rss tag
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            // The channel tag contains RSS feed details
            writer.WriteStartElement("channel");
            writer.WriteElementString("title", "FilStartUps Newest Submissions");
            writer.WriteElementString("link", "http://www.filstartups.com");
            writer.WriteElementString("description",
              "The latest articles.");
            writer.WriteElementString("copyright",
              String.Format("Copyright {0} FilStartUp. All rights reserved.", DateTime.Today.Year));

            // File Parade image    
            writer.WriteStartElement("image");
            writer.WriteElementString("url",
              "http://www.fileparade.com/Images/logo88x31.png");
            writer.WriteElementString("title",
              "FilStartUps Latest Articles");
            writer.WriteElementString("link", "http://www.filstartups.com");
            writer.WriteEndElement();
            // Objects needed for connecting to the SQL database
            var latest = FilStartUpsAPI.GetLatestArticles();
            foreach (var item in latest)
            {
                writer.WriteStartElement("item");

                writer.WriteElementString("title", item.posttitle);

                writer.WriteElementString("description", item.postdescription);

                writer.WriteElementString("link", "hello");

                writer.WriteElementString("pubDate", item.datecreated.ToShortDateString());

                writer.WriteEndElement();
            }

            // Close all tags
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
            // Terminate response
            Response.End();

        }


        protected string FormatForXML(object input)
        {
            string data = input.ToString();      // cast the input to a string

            // replace those characters disallowed in XML documents
            data = data.Replace("&", "&amp;");
            data = data.Replace("\"", "&quot;");
            data = data.Replace("'", "&apos;");
            data = data.Replace("<", "&lt;");
            data = data.Replace(">", "&gt;");

            return data;
        }
    }
}